﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MINERP.InfoConfig.Controller;
using MINERP.DAL;

namespace MINERP.InfoConfig
{
    public partial class FrmProductCategoryManager : FrmMDIChildFormTemplate
    {
        private ProductCategoryController _controller = new ProductCategoryController();
        private Tb_PorductCategory _productCategory = null;
        private static FrmProductCategoryManager instance = null;
        private FrmProductCategoryManager()
        {
            InitializeComponent();
        }


        public static FrmProductCategoryManager CreateInstance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FrmProductCategoryManager();
                }
                return instance;
            }
        }

        private void FrmProductCategoryManager_Load(object sender, EventArgs e)
        {
            //_controller.Grid = gridControlProductCategory;
            //_controller.View = gridViewProductCategory;
            //_controller.GetData();
            bingding();
        }
        void bingding()
        {
            List<Tb_PorductCategory> list = new List<Tb_PorductCategory>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                foreach (Tb_PorductCategory porductCategory in db.Tb_PorductCategory)
                {
                    list.Add(porductCategory);
                }
            }
            gridControlProductCategory.DataSource= list;
        }
        private void gridViewProductCategory_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _productCategory = (Tb_PorductCategory)gridViewProductCategory.GetFocusedRow();
            if (_productCategory != null)
            {
                txtProductCategoryName.Text = _productCategory.ProductCategoryName;
                txtProductCategoryDes.Text = _productCategory.ProductCategoryDes;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text != "保存")
            {
                PrepareAdd(true);
                txtProductCategoryName.Focus();
                return;
            }
            if (CheckData())
            {
                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                {
                    Tb_PorductCategory categoryInDB = db.Tb_PorductCategory.FirstOrDefault(T => T.ProductCategoryName == txtProductCategoryName.Text);
                    if (categoryInDB == null)
                    {
                        Tb_PorductCategory Category = new Tb_PorductCategory();
                        Category.ProductCategoryName = txtProductCategoryName.Text;
                        Category.ProductCategoryDes = txtProductCategoryDes.Text;
                        db.AddToTb_PorductCategory(Category);
                        db.SaveChanges();
                    }
                    else
                    {
                        MessageBox.Show("该产品分类已经存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }

                //link datagrid
                _controller.Grid = gridControlProductCategory;
                _controller.View = gridViewProductCategory;
                _controller.GetData();

                PrepareAdd(false);
                MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (btnUpdate.Text != "保存")
            {
                PrepareUpdate(true);
                txtProductCategoryName.Focus();
                return;
            }
            if (CheckData())
            {
                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                {
                    Tb_PorductCategory categoryInDB = db.Tb_PorductCategory.FirstOrDefault(T => T.ProductCategoryName == txtProductCategoryName.Text);

                    Tb_PorductCategory Category = db.Tb_PorductCategory.FirstOrDefault(p=>p.ProductCategoryId==_productCategory.ProductCategoryId);
                    if (categoryInDB != null && categoryInDB.ProductCategoryId != Category.ProductCategoryId)
                    {
                        MessageBox.Show("该产品分类已经存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    else
                    {
                        Category.ProductCategoryName = txtProductCategoryName.Text;
                        Category.ProductCategoryDes = txtProductCategoryDes.Text;
                        //db.a
                    }
                    db.SaveChanges();
                }

                //refresh datagrid
                //_controller.Grid = gridControlProductCategory;
                //_controller.View = gridViewProductCategory;
                //_controller.GetData();
                bingding();
                PrepareUpdate(false);

                MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            } 
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("确认删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
            {
                //Tb_PorductCategory Category = _controller.GetProductCategoryObj(_productCategory.ProductCategoryId);
                //_controller.Delete(Category);

                ////refresh datagrid
                //_controller.Grid = gridControlProductCategory;
                //_controller.View = gridViewProductCategory;
                //_controller.GetData();
                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                {
                    Tb_PorductCategory categoryInDB = db.Tb_PorductCategory.FirstOrDefault(T => T.ProductCategoryName == txtProductCategoryName.Text);
                    db.DeleteObject(categoryInDB);
                    db.SaveChanges();
                }
                bingding();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtProductCategoryName.Enabled = false;
            txtProductCategoryDes.Enabled = false;

            btnAdd.Text = "添加";
            btnUpdate.Text = "更新";
            btnAdd.Enabled = true;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
        }

        /// <summary>
        /// 检查数据完整性
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            bool flag = true;
            if (txtProductCategoryName.Text == string.Empty)
            {
                txtProductCategoryName.Focus();
                MessageBox.Show("产品类型名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (txtProductCategoryDes.Text == string.Empty)
            {
                txtProductCategoryDes.Focus();
                MessageBox.Show("备注不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return flag;
        }
        /// <summary>
        /// 添加前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareAdd(bool bl)
        {
            txtProductCategoryName.Enabled = bl;
            txtProductCategoryDes.Enabled = bl;

            txtProductCategoryName.Text = string.Empty;
            txtProductCategoryDes.Text = string.Empty;
            if (bl)
            {
                btnAdd.Text = "保存";
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btnAdd.Text = "添加";
                btnUpdate.Enabled = true;
                btnDelete.Enabled = true;
            }
        }

        /// <summary>
        /// 更新前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareUpdate(bool bl)
        {
            txtProductCategoryName.Enabled = bl;
            txtProductCategoryDes.Enabled = bl;

            if (bl)
            {

                btnUpdate.Text = "保存";
                btnAdd.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btnUpdate.Text = "更新";
                btnAdd.Enabled = true;
                btnDelete.Enabled = true;
            }
        }

        private void FrmProductCategoryManager_FormClosed(object sender, FormClosedEventArgs e)
        {
            instance = null;
        }

    }
}
